home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / s0ftpj / gork.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-17  |  22.9 KB  |  958 lines

  1. /*
  2.  * G o r k   -  U n i x   P a c k e t   L o g g e r
  3.  * 
  4.  * c o m p a t i b l e  v e r s i o n
  5.  *
  6.  *
  7.  * Compile with: gcc gork.c -O4 -Wall -lpcap
  8.  *
  9.  * Tested on:
  10.  *
  11.  *     Linux RedHat 6.X     ( mail.cameretta.pig )
  12.  *    Linux Debian 2.0r3   ( porcellino.cameretta.pig ) 
  13.  *      FreeBSD 4.0          ( sp00f.y0ur.life.cameretta.pig )
  14.  * 
  15.  *
  16.  * Gork is a tcp/udp/icmp/ip dumper with options to log only packets     
  17.  * from/to specific machine/s in a file with name = its ip/hostname  
  18.  * This version supports pcap library                            
  19.  * so this code can be compiled in other boxes                       
  20.  * How can i log?                                                   
  21.  * For example we can use a config file [gork.conf] like this:
  22.  * whitehouse.gov                                    
  23.  * 192.168.1                                       
  24.  * www.dead.net        <---- great grateful stuff!!!                   
  25.  * baubau.bau                                                                  
  26.  *                                           
  27.  * And we see every packets from/to whitehouse.gov in whitehouse.gov file      
  28.  * from/to 192.168.1.XXX in 192.168.1 file and so on ....                      
  29.  * NOTE: To log all pkt use a "." in gork.conf file... Output will be in       
  30.  * all_g0rk.log                                                   
  31.  *                                                                   
  32.  * Gork can be used to log pkt from / to a particular port:               
  33.  *                                                           
  34.  * gork -p port                                           
  35.  * or to check for a particular syslog msg                                     
  36.  * gork -l refused                                   
  37.  * And you can see result in gork.syslog                                   
  38.  *                                                               
  39.  * usage: gork -h for help ...                                   
  40.  *                                           
  41.  * Greetings go to: s0ftPj, MTV (gork was created after Beavis & Butthead show)
  42.  * b0z0/iKX, PacketStorm, all members of Metro Olografix (a cool organization),
  43.  * dislessici, dead.net for their stuffs, Sky, Francesca & other my friends... 
  44.  *
  45.  *
  46.  * pIGpEN <pigpen@s0ftpj.org>
  47.  */
  48.  
  49.  /*  Wake of the flood, laughing water,  forty-nine. Get out the pans.
  50.      Don't just stand the dreamin'. Get out the way. Get out the way.
  51.              Here comes sunshine, Here comes sunshine */
  52.  
  53. // CONFIGURATION 
  54.  
  55. /*
  56.  *
  57.  * DONT_LOOKUP:
  58.  *         gork uses gethostbyaddr() function to obtain hostnames: this
  59.  *         can create a lot of traffic from your box to your name server
  60.  *         and vice versa so you can disable lookup with DONT_LOOKUP
  61.  *        but hostnames or strings in gork.conf can't be considered
  62.  *
  63.  *        NOTE: gork has a little filter for this.. look for yes_lookup
  64.  *              flag
  65.  *         
  66.  */
  67.  
  68. //#define DONT_LOOKUP    //    if you have problem with gork & your dns
  69.  
  70. /*
  71.  * PROMISC:
  72.  *            value:   0    --> NO PROMISC 
  73.  *                              (you see only packets from/to your box)
  74.  *                    1    --> PROMISC MODE
  75.  *                             (you see all your network packets)  
  76.  */
  77.  
  78. #define PROMISC  1  
  79.  
  80. /*
  81.  * SYSTEM LOG:
  82.  *         gork can send msg via syslog... This isn't a good thing if
  83.  *         you send syslog msg to another host... and can also gives
  84.  *         you problem with console output
  85.  */
  86.  
  87. //#define SYSTEM_LOG
  88.  
  89. /* 
  90.  *
  91.  * G 0 R K  a porting of Timothy Leary on your Box ... ;)
  92.  * 
  93.  */
  94.  
  95. #include         <stdio.h>
  96. #include         <netdb.h>
  97. #include         <string.h>
  98. #include         <stdlib.h>
  99. #include         <unistd.h>
  100. #include         <arpa/nameser.h>
  101. #include         <sys/ioctl.h>
  102. #include         <sys/types.h>
  103. #include         <sys/time.h>
  104. #include         <sys/signal.h>
  105. #include         <sys/socket.h>
  106. #include         <net/bpf.h>
  107. #include        <netinet/in_systm.h>
  108. #include         <netinet/in.h>
  109. #include         <pcap.h>        
  110. #if (linux)
  111. #define __FAVOR_BSD
  112. #endif
  113. #include        <netinet/ip.h>
  114. #include        <netinet/ip_icmp.h>
  115. #include        <netinet/udp.h>
  116.  
  117. #define            CONF        "gork.conf"
  118. #define            LOG_TYPE    4    //syslog
  119.  
  120. #define            SYSLOG_PORT    514
  121. #define            MTU        1500
  122. #define            URG        32
  123. #define            ACK_PSH        24
  124. #define            SYN_ACK        18
  125. #define            FIN_ACK        17
  126. #define            ACK        16
  127. #define                 PSH             8
  128. #define                 RST             4
  129. #define                 SYN             2
  130. #define             FIN             1
  131.  
  132.  
  133. #define            WHITE        printf("\033[0;29m")
  134. #define                 RED             printf("\033[1;31m")
  135. #define             GREEN           printf("\033[1;32m")
  136. #define                YELLOW          printf("\033[1;33m")
  137. #define            BLUE        printf("\033[1;34m")
  138. #define            MAGENTA        printf("\033[1;35m")
  139. #define             CYAN            printf("\033[1;36m")
  140. #define            RETURN        printf("\n")
  141. #define            CLEAR        printf("\033[2J\033[1;1H")
  142. #define            LINE        printf("<\033[1;32m-----\033[1;34m>\n");
  143.  
  144. #define         ADDR_DIM    300
  145. #define            LOG_ALL        "all_g0rk.log"
  146. #define            LOG_SL        "gork.syslog"
  147.  
  148. unsigned char         s_addr[ADDR_DIM];
  149. unsigned char       d_addr[ADDR_DIM];
  150. char             *hst_saddr=NULL;
  151. char             *hst_daddr=NULL;
  152. time_t             now;
  153. char             date[60];
  154. int             port=0;
  155. char            *syslog_string;
  156.  
  157. extern char         *optarg;
  158.  
  159. pcap_t                 *pcap_global_descriptor;
  160. char                   *deviceglobal=NULL;
  161. int                    offset;
  162.  
  163. /* Line up a longshot, maybe try it two times, maybe more.
  164.    Good to know you got shoes to wear,
  165.    when you find the floor. Why hold out for more? 
  166.            Here comes sunshine, here comes sunshine!! */
  167.  
  168. struct packet_info{
  169.     unsigned char ttl, protocol, version;
  170.     unsigned char *saddr, *daddr;
  171.     unsigned long seq, ack_seq;
  172.     unsigned short source, dest, type, id, flags, window;
  173.     char dataload[2000];
  174. };
  175.  
  176. struct DNSpkt {
  177.  HEADER head;
  178.  char query[255];
  179. };
  180.  
  181. struct TCPhdr {
  182.  u_short source, dest;
  183.  u_int32_t seq, ack_seq;
  184.  u_short offset_flag, window, checksum, urgent;
  185. };
  186.  
  187. // think different not so much ...
  188.  
  189. int         main             __P((int, char **));
  190. unsigned long     in_aton         __P((const char *));
  191. void         usage             __P((char *));
  192. void         scan             __P((struct packet_info));
  193. void         fuckin_about_all_day     __P((void));
  194. void         print_addr         __P((struct packet_info));
  195. void         pcap_device_on         __P((void));
  196. void         sniff_pk         __P((struct packet_info *));
  197. void         ethclose         __P(());
  198. void         dump_tcp         __P((struct packet_info, int));
  199. void         dump_udp         __P((struct packet_info));
  200. void         dump_icmp         __P((struct packet_info));
  201.  
  202. #ifndef DONT_LOOKUP
  203.  
  204. char         *hostLookup         __P((unsigned long));
  205.  
  206. char *hostLookup(unsigned long in)
  207. {
  208.  struct in_addr addr;
  209.  struct hostent *hostEnt;
  210.  
  211.  addr.s_addr = in;
  212.  hostEnt = gethostbyaddr((char *)&addr, sizeof(struct in_addr),AF_INET);
  213.  
  214.  if(!hostEnt) return NULL;
  215.            return (hostEnt->h_name);
  216. }
  217.  
  218. #endif
  219.  
  220. unsigned long in_aton(const char *str) {
  221.  unsigned long l;
  222.  unsigned int val;
  223.  int i;
  224.  
  225.  l = 0;
  226.  for (i = 0; i < 4; i++) {
  227.   l <<= 8;
  228.   if (*str != '\0') {
  229.    val = 0;
  230.    while (*str != '\0' && *str != '.') {
  231.     val *= 10;
  232.     val += *str - '0';
  233.     str++;
  234.    }
  235.    l |= val;
  236.    if (*str != '\0')
  237.     str++;
  238.   }
  239.  }
  240.  return(htonl(l));
  241. }
  242.  
  243. void usage(char *arg)
  244. {
  245.  YELLOW;
  246.  printf("\t\t\t  pIGpEN - diGiTaL dEAdhEAd -");
  247.  BLUE;
  248.  printf("\n\n\nPut hostname/ip in gork.conf ... \nUse ");
  249.  MAGENTA;
  250.  printf("-p dest_port ");
  251.  BLUE;
  252.  printf("if you wanna log only packets to dest_port\n and with ip/hostname in");
  253.  MAGENTA;
  254.  printf(" gork.conf\n");
  255.  BLUE;  
  256.  printf("To log all source ip put in gork.conf: ");
  257.  MAGENTA;
  258.  printf(".\n");
  259.  printf("-l string ");
  260.  BLUE;
  261.  printf("write in gork.syslog if <string> was found in syslog messages\n");
  262.  printf("Other options: \n");
  263.  printf("        -i interface\n");
  264.  printf("        -v verbose mode for tcp\n");
  265.  WHITE;
  266. }
  267.  
  268. /*
  269.     Askin' you nice now, keep the mother rollin' one more time.
  270.     Been down before, you just don't have to go no more,
  271.     No More.
  272.  
  273.     Here comes sunshine! Here comes sunshine! 
  274. */
  275.  
  276. void fuckin_about_all_day(void)
  277. {
  278.  CLEAR;
  279.  fflush(stdout); sleep(1);
  280.  printf("\033[1;35m     .g#S$'$S#n.\n");
  281.  printf("\033[1;35m     $$$$$ $$$$'\n");
  282.  printf("\033[1;35m     $$$$$\n");
  283.  printf("\033[1;35m     `$$$$$$$$$n\n");
  284.  printf("\033[1;34m           $$$$$\n");
  285.  printf("\033[1;34m     $$$$$ $$$$$\n");
  286.  printf("\033[1;34m     `$$$$s$$$S'\n\n");
  287.  fflush(stdout); sleep(1);
  288.  printf("\033[1;35m     .g#S$'$S#n.\n");
  289.  printf("\033[1;35m     $$$$$ $$$$$\n");
  290.  printf("\033[1;35m     $$$$$ $$$$$\n");
  291.  printf("\033[1;35m     $$$$$ $$$$$\n");
  292.  printf("\033[1;34m     $$$$$s$$$$'\n");
  293.  printf("\033[1;34m     $$$$$      \n");
  294.  printf("\033[1;34m     $$$$       \n");
  295.  fflush(stdout); sleep(1);
  296.  printf("\033[1;35m            S#n.\n");
  297.  printf("\033[1;35m            $$$$\n");
  298.  printf("\033[1;35m            $$$$\n");
  299.  printf("\033[1;35m            $$$$\n");
  300.  printf("\033[1;34m            $$$$\n");
  301.  printf("\033[1;34m      $$$$$ $$$$\n");
  302.  printf("\033[1;34m      `$$$$s$$S'\n\n");
  303.  fflush(stdout); sleep(1);
  304.  
  305.  MAGENTA;
  306.  printf("\033[15A\t\t\t\t     _____________________\n");
  307.  fflush(stdout); sleep(1);
  308.  printf("\033[01A\t\t\t\t     s o f t p r o j e c t\n\n");
  309.  fflush(stdout); sleep(1);
  310.  BLUE;
  311.  printf("\t\t\t  ____________________________________________\n\n");
  312.  fflush(stdout); sleep(1);
  313.  printf("\t\t\t\033[02A  d i g i t a l  s e k u r i t y  f o r  y 2 k\n\n");
  314.  fflush(stdout); sleep(1);
  315.  printf("\t\t\t\t  ___________________________\n");
  316.  fflush(stdout); sleep(1);
  317.  printf("\t\t\t\t\033[01A  w w w . s 0 f t p j . o r g\n");
  318.  fflush(stdout); sleep(1);
  319.  sleep(3);
  320.  CLEAR;
  321. }
  322.  
  323. void print_addr(struct packet_info infoz)
  324. {
  325.  struct servent  *service;
  326.  struct protoent *proto;
  327.  int yes_lookup=0;
  328.  
  329.  now=time(NULL);
  330.  strftime(date,60,"%H:%M:%S %a %h %d", localtime(&now));
  331.  
  332.  bzero(s_addr,sizeof(s_addr)); 
  333.  bzero(d_addr,sizeof(d_addr));
  334.  hst_daddr=hst_saddr=NULL;
  335.  
  336.  sprintf(s_addr,"%u.%u.%u.%u",
  337.                 infoz.saddr[0],
  338.                 infoz.saddr[1],
  339.                 infoz.saddr[2],
  340.                 infoz.saddr[3]);
  341.  
  342.  sprintf(d_addr,"%u.%u.%u.%u",
  343.                 infoz.daddr[0],
  344.                 infoz.daddr[1],
  345.                 infoz.daddr[2],
  346.                 infoz.daddr[3]);
  347.  
  348.  GREEN;     printf("%s\n",date);
  349.  BLUE;         printf("%s",s_addr);
  350.  GREEN;        printf(" -> ");
  351.  MAGENTA;    printf("%s",d_addr);
  352.  
  353.  if(infoz.protocol!=IPPROTO_ICMP) 
  354.  {
  355.   if((proto=getprotobynumber(infoz.protocol))) 
  356.    {
  357.     BLUE;       
  358.     if((service=getservbyport(infoz.source,proto->p_name)))
  359.              printf(" %s",service->s_name);
  360.     else     printf(" %d",infoz.source);
  361.     YELLOW;     printf(" / ");
  362.     MAGENTA;
  363.     if((service=getservbyport(infoz.dest,proto->p_name)))
  364.              printf("%s",service->s_name);
  365.     else     printf("%d",infoz.dest);
  366.    }
  367.  }
  368.  
  369. #ifndef DONT_LOOKUP 
  370.   BLUE; 
  371.  
  372.   // limit shit for dns ... invoked with gethostbyaddr() 
  373.   // you can change it as you want ...
  374.   
  375.   switch(infoz.protocol) 
  376.    {
  377.     case IPPROTO_TCP:
  378.                  if(infoz.flags==SYN)
  379.                    yes_lookup=1;
  380.               break;
  381.     case IPPROTO_UDP:
  382.               if(infoz.source != NAMESERVER_PORT &&
  383.                  infoz.dest   != NAMESERVER_PORT)
  384.                yes_lookup=1;
  385.               break;
  386.     case IPPROTO_ICMP:
  387.               yes_lookup=1;
  388.               break;
  389.   }          
  390.   
  391.  if(yes_lookup)
  392.  {     
  393.   RETURN;    
  394.   if(!(hst_saddr=hostLookup(in_aton(s_addr))))
  395.           printf("none -> ");
  396.   else
  397.           printf("%s -> ",hst_saddr);
  398.  
  399.   MAGENTA;
  400.            
  401.   if(!(hst_daddr=hostLookup(in_aton(d_addr))))
  402.           printf("none");
  403.   else
  404.           printf("%s",hst_daddr);
  405.  }  
  406. #endif
  407.  MAGENTA; RETURN;
  408.  
  409.  scan(infoz);    
  410. }
  411.  
  412.  
  413. void scan(struct packet_info infoz)
  414. {
  415.  FILE *iff, *of;
  416.  char buf[512];
  417.  char o[400],tmp_port[10]; 
  418.  char *flags=NULL;
  419.  int HST_FOUND=0;
  420.  
  421.  if(!(iff=fopen(CONF,"r")))
  422.    return;
  423.  while(fgets(buf,512,iff))
  424.   {
  425.    if(buf[strlen(buf)-1]=='\n')
  426.     buf[strlen(buf)-1]=0;
  427.  
  428.    if(infoz.version!=4)
  429.     {
  430.      CLEAR;
  431.      printf("Sorry this is only a ipv4 version. write to: pigpen@s0ftpj.org\n");
  432.      printf("if you wanna ipv6 support\n");
  433.      exit(-1);
  434.     } 
  435.  
  436.    if(port && infoz.dest!=port && infoz.source!=port)
  437.     return;
  438.  
  439.    if(port && (infoz.protocol==IPPROTO_ICMP))
  440.     return;  
  441.  
  442.    if(hst_saddr) {
  443.     if(strstr(hst_saddr,buf)) 
  444.      HST_FOUND=1;
  445.    }
  446.             
  447.    if(hst_daddr) {    
  448.     if(strstr(hst_daddr,buf))
  449.      HST_FOUND=1;
  450.    }
  451.     
  452.    if(  
  453.        strstr(s_addr,buf)     ||
  454.        strstr(d_addr,buf)     ||
  455.        HST_FOUND==1    
  456.      )     
  457.     {
  458. #ifdef SYSTEM_LOG
  459.       syslog(LOG_TYPE,"G0RK: My lord %s is here ... I log it", s_addr);
  460. #endif
  461.      if(buf[0]=='.' && buf[1]=='\0')
  462.       of=fopen(LOG_ALL,"a");
  463.      else
  464.       (buf[0]=='.') ? bcopy(buf+1,o,sizeof(o)) : bcopy(buf,o,sizeof(o));
  465.      if(port) { sprintf(tmp_port,":%d",port); strncat(o,tmp_port,sizeof(o)); }
  466.      of=fopen(o,"a");
  467.      if(!of)
  468.       {
  469.        CLEAR;
  470.        printf("Can't open %s file\n\n\n",buf);
  471.        exit(-1);
  472.       }
  473.      fprintf(of,"<--->\n");
  474.      fprintf(of,"date: %s\n",date);
  475.      fprintf(of,"src:%s (%s)\ndst:%s (%s)\n",s_addr,hst_saddr,d_addr,hst_daddr);
  476.      if((infoz.protocol)!=IPPROTO_ICMP)
  477.        fprintf(of,"port: %d:%d\n",infoz.source,infoz.dest);
  478.      
  479.      switch(infoz.protocol)
  480.       {
  481.       case IPPROTO_ICMP:
  482.        fprintf(of,"type: ");
  483.        switch((infoz.type)/256) {
  484.         case 0:
  485.          fprintf(of,"icmp echo reply\n");
  486.          break;
  487.         case 3:
  488.          fprintf(of,"icmp dest_unreach\n");
  489.          break;
  490.         case 4:
  491.          fprintf(of,"icmp source quench\n");
  492.          break;
  493.         case 5:
  494.          fprintf(of,"icmp redirect\n");
  495.          break;
  496.         case 8:
  497.          fprintf(of,"icmp echo\n");
  498.          break;
  499.         case 11:
  500.          fprintf(of,"icmp time exceeded\n");
  501.          break;
  502.         case 12:
  503.          fprintf(of,"icmp parameter problem\n");
  504.          break;
  505.         case 13:
  506.          fprintf(of,"icmp timestamp\n");
  507.          break;
  508.         case 14:
  509.          fprintf(of,"icmp timestamp reply\n");
  510.          break;
  511.         case 15:
  512.          fprintf(of,"icmp information\n");
  513.          break;
  514.         case 16:
  515.          fprintf(of,"icmp information reply\n");
  516.          break;
  517.         case 17:
  518.          fprintf(of,"icmp address mask\n");
  519.          break;
  520.         case 18:
  521.          fprintf(of,"icmp address mask reply\n");
  522.          break;
  523.         default:
  524.          fprintf(of,"icmp type %i\n", infoz.type);
  525.          break;
  526.        }
  527.        break;  
  528.     
  529.       case IPPROTO_TCP:
  530.        fprintf(of,"seq #: %u\n",(unsigned int) infoz.seq);
  531.        fprintf(of,"ack #: %u\n",(unsigned int) infoz.ack_seq);
  532.        fprintf(of,"ttl: %i\n",infoz.ttl);
  533.        fprintf(of,"win: %i\n",infoz.window);
  534.        
  535.        switch (infoz.flags) {
  536.         case URG:
  537.          flags="-----U";
  538.          break;
  539.         case ACK_PSH:
  540.          flags="---PA-";
  541.          break;
  542.         case SYN_ACK:
  543.          flags="-S--A-";
  544.          break;
  545.         case FIN_ACK:
  546.          flags="F---A-";
  547.          break;
  548.         case ACK:
  549.          flags="----A-";
  550.          break;
  551.         case PSH:
  552.          flags="---P--";
  553.          break;
  554.         case RST:
  555.          flags="--R---";
  556.          break;
  557.         case SYN:
  558.          flags="-S----";
  559.          break;
  560.         case FIN:
  561.          flags="F-----";
  562.          break;
  563.         default:
  564.          break;
  565.        }
  566.        fprintf(of,"flags %s\n",flags);    
  567.        break;
  568.  
  569.       case IPPROTO_UDP:
  570.        if(infoz.dest==SYSLOG_PORT && port!=SYSLOG_PORT)
  571.         fprintf(of,"SYSLOG DATA: %s\n",infoz.dataload); 
  572.        break;
  573.  
  574.       default:
  575.        break;        
  576.       }//switch    
  577.     
  578.     buf[strlen(buf)+1]=0;
  579.     buf[strlen(buf)]='\n';
  580.  
  581.     fclose(of);
  582.    }
  583.  }
  584.  fclose(iff);
  585. }
  586.  
  587.  
  588. void pcap_device_on(void)
  589. {
  590.  char            errbuf[1028];
  591.  struct pcap_pkthdr pcap_hdr;
  592.  int datalink;
  593.  
  594.  if (!deviceglobal || !strcmp(deviceglobal, "default")) {
  595.   deviceglobal=pcap_lookupdev(errbuf);
  596.   printf("Device detected ->");
  597.   GREEN;
  598.   printf(" %s.\n\n", deviceglobal);
  599.  }
  600.  
  601.  if (!deviceglobal) {
  602.   printf("Error getting device - %s\n", errbuf);
  603.   exit(1);
  604.  }
  605.  
  606.  pcap_global_descriptor =
  607.   pcap_open_live(deviceglobal, 68, PROMISC, 1000, errbuf);
  608.  
  609.  if (!pcap_global_descriptor) {
  610.   printf("error opening pcap: %s\n", errbuf);
  611.   exit(1);
  612.  }
  613.  
  614.  datalink = pcap_datalink(pcap_global_descriptor);
  615.  bzero(&pcap_hdr, sizeof(struct pcap_pkthdr));
  616.  
  617.  switch (datalink) {
  618.   case DLT_EN10MB:
  619.                   offset = 14;
  620.                   break;
  621.   case DLT_NULL:
  622.   case DLT_PPP:    
  623.                   offset = 4;
  624.                   break;
  625.   case DLT_SLIP:
  626.                   offset = 16;
  627.                   break;
  628.   case DLT_RAW:
  629.           offset = 0;
  630.           break;
  631.   case DLT_SLIP_BSDOS:
  632.   case DLT_PPP_BSDOS:
  633.           offset = 24;
  634.           break;
  635.   default:
  636.                   printf("unknown datalink type (%d)", datalink);
  637.                   exit(-1); 
  638.  }
  639. }
  640.  
  641.  
  642. void sniff_pk(struct packet_info *infoz)
  643. {
  644.  struct ip          *IP;
  645.  struct TCPhdr      *TCP;
  646.  struct udphdr      *UDP;
  647.  struct icmp        *ICMP;
  648.  struct pcap_pkthdr lpcap_hdr;
  649.  char     *sniff_buff;
  650.  
  651.  bzero(s_addr, sizeof(s_addr));
  652.  bzero(d_addr, sizeof(d_addr));
  653.  
  654.  if((sniff_buff=(char *) pcap_next(pcap_global_descriptor, &lpcap_hdr))){
  655.   (char *) sniff_buff+=offset;
  656.   IP = (struct ip *) sniff_buff;
  657.   infoz->ttl = IP->ip_ttl;
  658.   infoz->protocol = (char)IP->ip_p;
  659.   infoz->version  = (char)IP->ip_v;
  660.   infoz->saddr = (unsigned char *)&(IP->ip_src.s_addr);
  661.   infoz->daddr = (unsigned char *)&(IP->ip_dst.s_addr);
  662.  
  663.   switch (infoz->protocol) {
  664.    case IPPROTO_TCP:
  665.     TCP = (struct TCPhdr *)(sniff_buff+sizeof(*IP));
  666.     infoz->seq     = ntohl(TCP->seq);
  667.     infoz->ack_seq = ntohl(TCP->ack_seq);
  668.     infoz->source  = ntohs(TCP->source);
  669.     infoz->dest    = ntohs(TCP->dest);
  670.     infoz->window  = ntohs(TCP->window);
  671.     infoz->flags   = ntohs(TCP->offset_flag)&
  672.      (URG|ACK|PSH|FIN|RST|SYN);
  673.     memcpy(infoz->dataload,
  674.            sniff_buff + sizeof(struct ip) + sizeof(struct TCPhdr),
  675.            ntohs(IP->ip_len)-sizeof(struct ip)-sizeof(struct TCPhdr));
  676.     break;
  677.    case IPPROTO_UDP:
  678.     UDP = (struct udphdr *)(sniff_buff+sizeof(*IP));
  679.     infoz->source = ntohs(UDP->uh_sport);
  680.     infoz->dest   = ntohs(UDP->uh_dport);
  681.     memcpy(infoz->dataload,
  682.            sniff_buff + sizeof(struct ip) + sizeof(struct udphdr),
  683.            ntohs(IP->ip_len)-sizeof(struct ip)-sizeof(struct udphdr));
  684.     break;
  685.   case IPPROTO_ICMP:
  686.    ICMP = (struct icmp *)(sniff_buff+sizeof(*IP));
  687.    infoz->type = ntohs(ICMP->icmp_type);
  688.    infoz->id   = ntohs(ICMP->icmp_seq);
  689.    break;
  690.   default:
  691.    break;
  692.   }
  693.  }
  694. }
  695.  
  696.  
  697. void ethclose()
  698. {
  699.  if(pcap_global_descriptor) pcap_close(pcap_global_descriptor);
  700.  MAGENTA;
  701.  printf("I will getby ... I will survive...\n");
  702.  WHITE;
  703.  exit(0);
  704. }
  705.  
  706.  
  707. void dump_tcp(struct packet_info info, int data)
  708. {
  709.  char *flags=NULL;
  710.  
  711.  print_addr(info);
  712.  MAGENTA;
  713.  printf("TCP ");
  714.  BLUE;
  715.  printf("%u:", (unsigned int) info.seq);
  716.  MAGENTA;
  717.  printf("%u", (unsigned int) info.ack_seq);
  718.  MAGENTA; printf("\tTTL: ");
  719.  BLUE;    printf("%i ", info.ttl);
  720.  MAGENTA; printf("\tWin: ");
  721.  BLUE;    printf("%i", info.window);    
  722.  
  723.  switch (info.flags) {
  724.   case URG:
  725.    flags="-----\033[1;32mU\033[1;34m";
  726.    break;
  727.   case ACK_PSH:
  728.    flags="---\033[1;32mPA\033[1;34m-";
  729.    break;
  730.   case SYN_ACK:
  731.    flags="-\033[1;32mS\033[0;34m--\033[1;32mA\033[1;34m-";
  732.    break;
  733.   case FIN_ACK:
  734.    flags="\033[1;32mF\033[1;34m---\033[1;32mA\033[1;34m-";
  735.    break;
  736.   case ACK:
  737.    flags="----\033[1;32mA\033[1;34m-";
  738.    break;
  739.   case PSH:
  740.    flags="---\033[1;32mP\033[1;34m--";
  741.    break;
  742.   case RST:
  743.    flags="--\033[1;32mR\033[1;34m---";
  744.    break;
  745.   case SYN:
  746.    flags="-\033[1;32mS\033[1;34m----";
  747.    break;
  748.   case FIN:
  749.    flags="\033[1;32mF\033[1;34m-----";
  750.    break;
  751.   default:
  752.    break;
  753.  }
  754.  
  755.  MAGENTA;  printf("     FLAGS: ");
  756.  BLUE; printf("%s\n",flags);
  757.  if(data && (info.flags==PSH || info.flags==ACK_PSH))
  758.   { 
  759.    BLUE; printf("-> ");
  760.    GREEN;  printf("%s\n",info.dataload);
  761.   }
  762.  LINE;
  763. }
  764.  
  765.  
  766. void dump_udp(struct packet_info info)
  767. {
  768.  FILE *fp_sl;
  769.  struct DNSpkt *dns_pkt;
  770.  
  771.  print_addr(info);
  772.  printf("UDP ");
  773.  
  774.  if(info.dest==SYSLOG_PORT && port!=SYSLOG_PORT)
  775.   {
  776.    GREEN; 
  777.    printf("%s", info.dataload);
  778.    if(syslog_string && info.dataload)
  779.     {
  780.      if(strstr(info.dataload,syslog_string))
  781.       {
  782.        RED;
  783.        printf("\tMSG LOGGED -> %s\n",LOG_SL);
  784.        fp_sl=fopen(LOG_SL,"a");
  785.        now=time(NULL);
  786.        strftime(date,60,"%H:%M:%S %a %h %d", localtime(&now));
  787.        fprintf(fp_sl,"\n%s --- str -> %s\n",date,syslog_string); 
  788.        fprintf(fp_sl,"%s -> %s == %s\n", 
  789.                      s_addr,
  790.                      d_addr,
  791.                      info.dataload);
  792.        fclose(fp_sl);
  793.       }
  794.     }
  795.   } 
  796.  
  797.  if(info.source==NAMESERVER_PORT || info.dest==NAMESERVER_PORT) 
  798.   {
  799.    dns_pkt=(struct DNSpkt *) info.dataload;
  800.  
  801.    BLUE;    printf("\tRD: ");
  802.    MAGENTA; printf("%d ",dns_pkt->head.rd);
  803.    BLUE;    printf("AA: ");
  804.    MAGENTA; printf("%d ",dns_pkt->head.aa);
  805.    BLUE;    printf("OPCODE: ");
  806.    MAGENTA;
  807.    switch(dns_pkt->head.opcode)
  808.    {
  809.      case  QUERY: printf("QUERY ");  break;
  810.      case IQUERY: printf("IQUERY "); break;
  811.      case STATUS: printf("STATUS "); break;
  812.          default: printf("%d ",dns_pkt->head.opcode);  
  813.    }
  814.    BLUE;        printf("QR: ");
  815.    MAGENTA;     printf("%d ",dns_pkt->head.qr);
  816.  
  817.    BLUE;     printf("RA: ");
  818.    MAGENTA;    printf("%d ",dns_pkt->head.ra);
  819.    BLUE;        printf("AD: ");
  820.    MAGENTA;     printf("%d ",dns_pkt->head.ad);
  821.    BLUE;        printf("CD: ");
  822.    MAGENTA;     printf("%d",dns_pkt->head.cd);
  823.    BLUE;        printf("\tDNSPKT ID: ");
  824.    RED;         printf("%d",dns_pkt->head.id);
  825.   }
  826.  RETURN; BLUE; LINE;
  827. }
  828.  
  829.  
  830. void dump_icmp(struct packet_info info)
  831. {
  832.  print_addr(info);
  833.  
  834.  MAGENTA; printf("ICMP ");
  835.  BLUE;      printf("TYPE: ");
  836.  RED;
  837.  switch((info.type/256)) {
  838.   case 0:
  839.          printf("echo reply\t");
  840.          break;
  841.   case 3:
  842.          printf("dest_unreach\t");
  843.          break;
  844.   case 4:
  845.          printf("source quench\t");
  846.          break;
  847.   case 5:
  848.          printf("redirect\t");
  849.          break;
  850.   case 8:
  851.          printf("echo\t");
  852.          break;
  853.   case 11:
  854.          printf("time exceeded\t");
  855.          break;
  856.   case 12:
  857.          printf("parameter problem\t");
  858.          break;
  859.   case 13:
  860.          printf("timestamp\t");
  861.          break;
  862.   case 14:
  863.          printf("timestamp reply\t");
  864.          break;
  865.   case 15:
  866.          printf("information\t");
  867.          break;
  868.   case 16:
  869.          printf("information reply\t");
  870.          break;
  871.   case 17:
  872.          printf("address mask\t");
  873.          break;
  874.   case 18:
  875.          printf("address mask reply\t");
  876.          break;
  877.   default:
  878.          printf("%i\t", info.type);
  879.          break;
  880.  }
  881.  
  882.  BLUE;
  883.  printf("(ttl:%i id:%i)\n", info.ttl, (info.id/256));
  884.  LINE;
  885. }
  886.  
  887.  
  888. int main(int argc, char **argv)
  889. {
  890.  int snoop = 0, opt;
  891.  struct packet_info pk_info;
  892.  
  893.  signal(SIGINT,  ethclose);
  894.  signal(SIGTERM, ethclose);
  895.  signal(SIGKILL, ethclose);
  896.  signal(SIGQUIT, ethclose);
  897.  
  898.  fuckin_about_all_day(); 
  899.  
  900.  while ((opt = getopt(argc, (char **) argv, "vhp:l:i:")) != EOF) {
  901.   switch(opt)
  902.    {
  903.     case 'v':
  904.              snoop=1;
  905.              break;
  906.     case 'h':
  907.          usage(argv[0]);
  908.          exit(1);
  909.     case 'p':
  910.              if(syslog_string)
  911.           {
  912.            printf("Can't execute gork with -l & -p\n");
  913.            WHITE;
  914.            exit(1);
  915.           }
  916.          port=atoi(optarg);
  917.          break;
  918.     case 'l':
  919.              if(port)
  920.               {
  921.            printf("Can't execute gork with -p & -l\n");
  922.            WHITE;
  923.            exit(1);
  924.           } 
  925.              syslog_string=optarg;
  926.          break;    
  927.     case 'i':
  928.          deviceglobal=optarg;
  929.          break;
  930.     default:
  931.             exit(1);
  932.    }
  933.  }
  934.  
  935.  pcap_device_on();
  936.  
  937.  while(1) {
  938.   bzero(&pk_info,sizeof(pk_info));
  939.   sniff_pk(&pk_info);
  940.  
  941.   // add here other protocol implementations with their functions 
  942.   
  943.   switch(pk_info.protocol) {
  944.    case IPPROTO_TCP:
  945.                      dump_tcp(pk_info, snoop);
  946.                      break;
  947.    case IPPROTO_UDP:
  948.                      dump_udp(pk_info);
  949.                      break;
  950.    case IPPROTO_ICMP:
  951.                      dump_icmp(pk_info);
  952.                      break;
  953.              default:
  954.                      break; 
  955.   }
  956.  }
  957. }
  958.